package com.flow.asyncflow.web.controller;


import com.flow.asyncflow.core.entity.asyncFlowMonitor.AsyncFlowLogVo;
import com.flow.asyncflow.core.entity.asyncFlowMonitor.AsyncFlowMonitorVo;
import com.flow.asyncflow.core.entity.asyncFlowMonitor.QueryAsyncFlowLogRequest;
import com.flow.asyncflow.core.entity.codegeneration.VelocityModel;
import com.flow.asyncflow.core.entity.codegeneration.VelocityRequest;
import com.flow.asyncflow.core.enums.ExecuteEnum;
import com.flow.asyncflow.core.utils.asyncFlowMonitor.AsyncFlowMonitor;
import com.flow.asyncflow.core.utils.codegeneration.AsyncFlowVelocity;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/AsyncFlowAPI")
public class AsyncFlowAPIController {


    //region 异步流代码生成

    /**
     * 生成 异步流
     *
     * @param velocityRequest 请求
     * @return 返回
     */
    @SneakyThrows
    @PostMapping("codeGeneration")
    public String codeGeneration(@RequestBody VelocityRequest velocityRequest) {
        String result = "生成成功";
        //velocityRequest.setHasBaseContext(true);
        if (StringUtils.isBlank(velocityRequest.getAsyncFlowModuleName())) {
            result = "异步流模块英文名不能为空";
            return result;
        }
        if (StringUtils.isBlank(velocityRequest.getAsyncFlowModuleDes())) {
            result = "异步流中文描述不能为空";
            return result;
        }
        if (StringUtils.isBlank(velocityRequest.getAsyncFlowMaintainName())) {
            result = "异步流负责人";
            return result;
        }
        if (velocityRequest.getAsyncFlowTimeOutsLimit() <= 0) {
            result = "异步流超时时间必须大于0";
            return result;
        }
        velocityRequest.getAsyncFlowNodeInfoList().removeAll(velocityRequest.getAsyncFlowNodeInfoList().stream().filter(c -> StringUtils.isBlank(c.getAsyncFlowNodeFileName())).collect(Collectors.toList()));
        if (velocityRequest.getAsyncFlowNodeInfoList().isEmpty()) {
            result = "至少包含一个节点";
            return result;
        }
        AsyncFlowVelocity.buildAsyncFlowVelocity(velocityRequest);
        return result;

    }

    /**
     * 获取公共组件和管理器
     *
     * @return 返回
     */
    @SneakyThrows
    @PostMapping("loadPubNodeManager")
    public LinkedList<VelocityModel.AsyncFlowNodeInfo> loadPubNodeManager() {
        Yaml yaml = new Yaml();
        InputStream resourceAsStream = AsyncFlowVelocity.class.getClassLoader().getResourceAsStream("asyncflow.yml");
        Map obj = yaml.load(resourceAsStream);
        String basePackagePath = (String) obj.get("AsyncFlowExecutePackagePath");
        LinkedList<VelocityModel.AsyncFlowNodeInfo> pubAsyncFlowNodeInfoList;
        //String loadPubNodePackPath = basePackagePath + ".GetData.pubgetdata";
        //pubAsyncFlowNodeInfoList = AsyncFlowVelocity.loadNodeAndManager(ExecuteEnum.GetDataPub, loadPubNodePackPath);
        //String loadManagerPackPath = basePackagePath + ".Manager";
        //pubAsyncFlowNodeInfoList.addAll(AsyncFlowVelocity.loadNodeAndManager(ExecuteEnum.Manager, loadManagerPackPath));
        pubAsyncFlowNodeInfoList = AsyncFlowVelocity.loadNodeAndManagerV2(basePackagePath);
        return pubAsyncFlowNodeInfoList;

    }

    /**
     * 获取管理器下面所有的节点
     *
     * @param asyncFlowModuleName 节点名称
     * @return 节点集合
     */
    @SneakyThrows
    @GetMapping("loadManagerNode")
    public VelocityRequest loadManagerNode(@RequestParam("asyncFlowModuleName") String asyncFlowModuleName) {
        Yaml yaml = new Yaml();
        InputStream resourceAsStream = AsyncFlowVelocity.class.getClassLoader().getResourceAsStream("asyncflow.yml");
        Map obj = yaml.load(resourceAsStream);
        String basePackagePath = (String) obj.get("AsyncFlowExecutePackagePath");
        String loadManagerNodePackPath = basePackagePath + ".manager";
        return AsyncFlowVelocity.loadManagerNode(asyncFlowModuleName, loadManagerNodePackPath);

    }
    //endregion

    //region 异步流监控

    /**
     * 查询 监控信息
     *
     * @param queryAsyncFlowLogRequest 请求
     * @return 返回
     */
    @SneakyThrows
    @PostMapping("getAsyncFlowLogByPage")
    public AsyncFlowMonitorVo getAsyncFlowLogByPage(@RequestBody QueryAsyncFlowLogRequest queryAsyncFlowLogRequest) {

        return new AsyncFlowMonitor().getAsyncFlowLogByPage(queryAsyncFlowLogRequest);

    }
    /**
     * 查询 监控信息
     *
     * @return 返回
     */
    @SneakyThrows
    @PostMapping("getAsyncFlowNameList")
    public  List<String> getAsyncFlowNameList() {


        return new AsyncFlowMonitor().getAsyncFlowNameList();

    }

    //endregion
}
